package cn.fengshenghospital.service.impl;


import cn.fengshenghospital.entity.Menu;
import cn.fengshenghospital.mapper.MenuMapper;
import cn.fengshenghospital.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ImplMenuService implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private CacheManager cacheManager;
    //获取菜单导航
//    @Cacheable(value = "menu",keyGenerator = "myKeyGenerator",unless = "#result == null")
    @Override
    public List<Menu> queryMenu(Integer id) throws Exception {
        Cache cache = cacheManager.getCache("menu");
        System.out.println(id);
        List<Menu> menuList = null;
        if (cache.get("queryMenu[null]") == null){
            System.out.println("查询数据库");
            menuList = menuMapper.getMenuList();
            cache.put("queryMenu[null]",menuList);
            if (id != null){
                menuList = querySubItems(menuList,id);
            }
            return menuList;
        }
        System.out.println("查询缓存");
        menuList = (List<Menu>)cache.get("queryMenu[null]").get();
        if (id != null){
            System.out.println(id);
            menuList = querySubItems(menuList,id);
        }
        return menuList;
    }

    //根据id获取指定缓存信息
    public List<Menu> querySubItems(List<Menu> menuList,Integer id)throws Exception{
        List<Menu> menus = new ArrayList<>();
        menus.add(menuList.get(id));
        return menus;
    }



















}
